package atguigu.demo3

/**
  * Created by lzd on 2018/11/2.
  * list
  */
object demo3_9 {
  def main(args: Array[String]): Unit = {
    val digits = List(4, 2)

    // 右结合操作符
    println(9 :: List(4, 2))

    // Nil表示空列表
    println(9 :: 4 :: 2 :: Nil)

    9 :: (4 :: (2 :: Nil))


    println(sum(List(9, 4, 2)))


    println(sum1(List(9, 4, 2)))

    List(9, 4, 2).sum
  }

  def sum1(lst: List[Int]): Int = if (lst == Nil) 0 else lst.head + sum(lst.tail)

  def sum(lst: List[Int]): Int = lst match {
    case Nil => 0
    case h :: t => h + sum(t) // h is lst.head, t is lst.tail
  }
}
